import Vue from 'vue'
import Vuex from 'vuex'
import $mConfig from '@/config/index.config.js'
import $mRoutesConfig from '@/config/routes.config.js'
import $mRouter from '@/common/router.js'
import QQMapWx from '@/apis/QQMapWx'
import {
	getWxUserInfo
} from '@/apis/index.js'
Vue.use(Vuex)

// uni.setStorageSync("token", "oVG1b1Bgrb");
// uni.setStorageSync("openId", "oVG1b1Bgrb-l6jNYCUr6sYpQ5F-U");

const TOKEN = uni.getStorageSync("token") || "";
const OPENID = uni.getStorageSync("openId") || "";
const USER_INFO = uni.getStorageSync("userInfo") || {};
const CUSTOMER_INFO = uni.getStorageSync("customerInfo") || {};
const CITY_CODE = uni.getStorageSync("city_code") || ""
const Info_Data = uni.getStorageSync("userInfoData") || {};
const isZP = uni.getStorageSync("isZP") || 0;
const qrcode = uni.getStorageSync("qrcode") || "";
const userRole = uni.getStorageSync("userRole") || 1

const store = new Vuex.Store({
	state: {
		// 是否強制登录
		forcedLogin: $mConfig.forcedLogin,
		// 前端token
		token: TOKEN,
		// 用户openid
		openId: OPENID,
		// 用户信息 头像 昵称
		userInfo: USER_INFO,
		// 用户的商户信息
		customerInfo: CUSTOMER_INFO,
		userInfoData: Info_Data,
		// 默认城市				
		addressInfo: {
			city: '' || '北京',
			district: '',
			street: '',
			province: '',
			address: '',
			name: '',
			city_code: '',
			lat: '',
			lng: '',
		},
		//城市code
		city_code: CITY_CODE,
		// 招聘端标识
		isZP: isZP,
		// 邀请二维码
		qrcode: qrcode,
		// 当前用户身份 1:求职端，2:boss端
		userRole: userRole

	},
	getters: {
		// 用户是否登录
		hasLogin: state => {
			if (state.token) {
				return true;
			} else {
				return false
			}
		},
		//根据city_code 判断用户有无获取位置信息
		isLocation: state => {
			if (state.city_code) {
				return true;
			} else {
				return false
			}
		},
		isZP: state => {
			return state.isZP;
		}
	},
	mutations: {
		SET_TOKEN(state, token) {
			state.token = token;
			uni.setStorageSync("token", token);
		},
		SET_OPENID(state, openId) {
			state.openId = openId;
			uni.setStorageSync("openId", openId);
		},
		SET_USERINFO(state, userInfo) {
			state.userInfo = userInfo;
			uni.setStorageSync("userInfo", userInfo);
		},
		SET_CUSTOMERINFO(state, customerInfo) {
			state.customerInfo = customerInfo;
			uni.setStorageSync("customerInfo", customerInfo);
		},
		SET_LOGOUT(state) {
			state.token = "";
			uni.setStorageSync("token", "");
		},
		newCityFun(state, newCity) {
			state.city_code = newCity.city_code
			uni.setStorageSync("city_code", newCity.city_code);
			state.addressInfo.city = newCity.city
			state.addressInfo.district = newCity.district
			state.addressInfo.street = newCity.street
			state.addressInfo.province = newCity.province
			state.addressInfo.address = newCity.address
			state.addressInfo.name = newCity.name
			state.addressInfo.city_code = newCity.city_code
			state.addressInfo.lat = newCity.lat
			state.addressInfo.lng = newCity.lng
		},
		//基本信息
		SetUSER_InfoData(state, userInfoData) {
			state.userInfoData = userInfoData;
			uni.setStorageSync("userInfoData", userInfoData);
		},
		SET_ISZP(state, isZP) {
			state.isZP = isZP;
			uni.setStorageSync("isZP", isZP);
		},
		SET_QRCODE(state, qrcode) {
			state.qrcode = qrcode;
			uni.setStorageSync("qrcode", qrcode);
		},
		SET_USERROLE(state, role) {
			state.userRole = role
			uni.setStorageSync('userRole', role)
		}
	},
	actions: {
		// 登录过期 重新登录
		reLogin({
			commit
		}, info) {
			commit("SET_TOKEN", "");
			$mRouter.redirectTo({
				route: $mRoutesConfig.login
			});
		},
		getUserInfo({
			commit,
			state
		}) {
			if (!state.userInfo || !state.userInfo.id) {
				return
			}
			getWxUserInfo(state.userInfo.id).then(res => {
				commit("SET_USERINFO", res.data)
			})
		},
		getCity({
			dispatch
		}, context) {
			uni.getSetting({
				success(res) {
					// 如果没有授权
					if (!res.authSetting['scope.userLocation']) {
						// 向用户发起授权请求，弹框提示
						uni.authorize({ // 获取用户定位信息
							scope: "scope.userLocation", // 用户同意授权执行
							success() {
								//获取位置信息
								uni.getLocation({
									type: 'gcj02',
									success: (res) => {
										// 逆地址解析方法
										dispatch("reverseGeo", res)
									}
								})
							},
							// 若用户不同意授权，弹框提示
							fail(res) {
								let that = this;
								uni.showModal({
									title: '提示',
									content: '注意：请允许获取您的地理位置，否则部分功能不能使用！',
									success: function(res) {
										if (res.confirm) {
											uni.openSetting({
												success: function(dataAu) {
													console.log(dataAu,
														"openSetting"
													);
													if (dataAu
														.authSetting[
															"scope.userLocation"
														] == true) {
														//再次授权，调用wx.getLocation的API
														uni.getLocation({
															type: "wgs84",
															success: (
																res
															) => {
																// 逆地址解析方法
																dispatch
																	("reverseGeo",
																		res
																	)
															},
															fail(
																err
															) {
																//授权失败后，调用，可以在这儿调用下面的例子 再次进行授权
																console
																	.log(
																		err,
																		"eee"
																	);
																uni.showToast({
																	title: "授权未通过",
																	icon: "none",
																});
																that
																	.openAuthSetting();
															},
														});
													} else {
														uni.showToast({
															title: "授权失败",
															icon: "none"
														});
													}
												},
											});
										} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								});
							}
						})
					} else {
						// 有权限则直接获取
						uni.getLocation({
							type: 'wgs84',
							success: (res) => {
								dispatch("reverseGeo", res)
							},
							fail(error) {
								uni.showToast({
									title: '请勿频繁调用！',
									icon: 'none',
								})
								console.log('失败', error)
							}
						})
					}
				}
			})

		},
		// 地址逆解析
		reverseGeo({
			commit
		}, location) {
			// 逆地址解析方法
			QQMapWx.reverseGeocoder(location.longitude, location.latitude)
				.then(res => {
					var newCity = {}
					// 取到用户的定位城市，赋值传递出去
					newCity.city = res.result.address_component.city
					newCity.district = res.result.address_component.district
					newCity.street = res.result.address_component.street
					newCity.province = res.result.address_component.province
					newCity.address = res.result.address
					newCity.name = res.result.ad_info.name
					newCity.city_code = res.result.ad_info.city_code
					newCity.lat = res.result.location.lat
					newCity.lng = res.result.location.lng
					commit('newCityFun', newCity)
				}).catch(e => {
					console.log("地址逆解析失败", e)
				})
		},
		// 设置缓存海报二维码
		setPosterQrcode({
			commit
		}, qrcodeUrl) {
			commit("SET_QRCODE", qrcodeUrl)
		}
	}
})

export default store
